package me.zhengjie.modules.thumbnail.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.banner.domain.Banner;
import me.zhengjie.modules.thumbnail.domain.Thumbnail;
import me.zhengjie.modules.thumbnail.domain.vo.ThumbnailVo;
import me.zhengjie.modules.thumbnail.service.ThumbnailService;
import me.zhengjie.modules.thumbnail.service.dto.ThumbnailDto;
import me.zhengjie.modules.thumbnail.service.dto.ThumbnailQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

/**
* @author TianI
* @date 2022-02-14
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "房产项目缩略图管理")
@RequestMapping("/api/thumbnail")
public class ThumbnailController {

    private final ThumbnailService thumbnailService;


    @GetMapping("/getByCompanyId/{id}")
    @Log("前台APP获取首页项目缩略图")
    @ApiOperation("前台APP获取首页项目缩略图")
    public ResponseEntity queryThumbnail(@PathVariable Long id){
        List<ThumbnailVo> thumbnailVo = thumbnailService.queryThumbnail(id);
        return new ResponseEntity<>(thumbnailVo,HttpStatus.OK);
    }
    @GetMapping("/getByName/{name}")
    @Log("前台APP搜索名字获取首页项目缩略图")
    @ApiOperation("前台APP搜索名字获取首页项目缩略图")
    public ResponseEntity getByName(@PathVariable String name){
        List<ThumbnailVo> thumbnailVo = thumbnailService.getByName(name);
        return new ResponseEntity<>(thumbnailVo,HttpStatus.OK);
    }

    @GetMapping
    @Log("查询房产项目缩略图")
    @ApiOperation("查询房产项目缩略图")
    @PreAuthorize("@el.check('thumbnail:list')")
    public ResponseEntity query(ThumbnailQueryParam query, Pageable pageable){
        return new ResponseEntity<>(thumbnailService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增房产项目缩略图")
    @ApiOperation("新增房产项目缩略图")
    @PreAuthorize("@el.check('thumbnail:add')")
    public ResponseEntity create(@Validated @RequestBody ThumbnailDto resources){
        return new ResponseEntity<>(thumbnailService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改房产项目缩略图")
    @ApiOperation("修改房产项目缩略图")
    @PreAuthorize("@el.check('thumbnail:edit')")
    public ResponseEntity update(@Validated @RequestBody ThumbnailDto resources){
        thumbnailService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除房产项目缩略图")
    @ApiOperation("删除房产项目缩略图")
    @PreAuthorize("@el.check('thumbnail:del')")
    public ResponseEntity delete(@RequestBody Set<Long> ids) {
        thumbnailService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @PostMapping("/upload")
    public ResponseEntity<Object> importProcessData(@RequestParam("file") MultipartFile file) {
        Thumbnail thumbnail = thumbnailService.create(file);
        return new ResponseEntity<>(thumbnail.getThumbnailUrl(),HttpStatus.CREATED);
    }
    /*
    @Log("导出房产项目缩略图")
    @ApiOperation("导出房产项目缩略图")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('thumbnail:list')")
    public void download(HttpServletResponse response, ThumbnailQueryParam query) throws IOException {
        thumbnailService.download(thumbnailService.queryAll(query), response);
    }*/

}
